/**
 * $Id$ Copyright 2009-2012 Oak Pacific Interactive. All rights reserved.
 */
package com.shuqi.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.application.PasswordProcess;
import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.HttpCommonParamsUtils;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.FindPasswordByEmailParser;
import com.shuqi.model.parse.parser.PasswordSettingParser;

/**
 * 
 * 
 * @author <a href="mailto:">yaozhen</a>
 * @version 2014-2-11下午6:57:03
 */
public class FindPasswordModel extends ModelBase implements HttpRequestListener {

    public final static int TYPE_PHOE = 100;

    public final static int TYPE_MAIL = 101;

    private HttpRequest mHttpRequest;

    private Context mContext;

    private int mType = 0;

    private String mAccount;

    private String mPassword; // 通过手机找回密码，设置密码使用

    private String mVerifyCode;// 通过手机找回密码，设置密码使用

    public FindPasswordModel(Context context, int type) {
        this.mContext = context;
        this.mType = type;
    }

    public void setAccount(String account) {
        this.mAccount = account;
    }

    public void setPassword(String password) {
        this.mPassword = password;
    }

    public void setVerifyCode(String vcode) {
        this.mVerifyCode = vcode;
    }

    @Override
    public void action(int actionCode, Object object) {

        fireEvent(actionCode, object);
    }

    @Override
    public String getRequestUrl() {
        if (mType == TYPE_PHOE) {
            return Urls.getFindPasswordByPhoneUrl();
        } else if (mType == TYPE_MAIL) {
            return Urls.getFindPasswordByMailUrl();
        }
        return null;
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        List<BasicNameValuePair> params = HttpCommonParamsUtils.getAccountBasicParams();
        if (mType == TYPE_PHOE) {
            params.add(new BasicNameValuePair("phone", mAccount));
            params.add(new BasicNameValuePair("password", PasswordProcess.getLgPassword(mPassword)));
            params.add(new BasicNameValuePair("vcode", mVerifyCode));
            return params;
        } else if (mType == TYPE_MAIL) {
            params.add(new BasicNameValuePair("email", mAccount));
            return params;
        }
        return null;
    }

    public void loadDataByMail(Object... objs) {
        setAccount(String.valueOf(objs[0]));
        mHttpRequest =
                new HttpRequest(mContext, UrlManager.COLUMN_DOMAIN_ACCOUNT, getRequestUrl(), setParams(),
                        this);
        mHttpRequest.setDataParseAdapter(new FindPasswordByEmailParser());
        MyTask.runInBackground(mHttpRequest, true);
    }

    /**
     * 
     * @param objs 需要传入 phoneNumber, pwd, verifyCode
     */
    public void loadDataByPhone(Object... objs) {
        setAccount(String.valueOf(objs[0]));
        setPassword(String.valueOf(objs[1]));
        setVerifyCode(String.valueOf(objs[2]));
        mHttpRequest =
                new HttpRequest(mContext, UrlManager.COLUMN_DOMAIN_ACCOUNT, getRequestUrl(), setParams(),
                        this);
        mHttpRequest.setDataParseAdapter(new PasswordSettingParser());
        MyTask.runInBackground(mHttpRequest, true);
    }

    @Override
    public void onDestroy() {
        if (mHttpRequest != null) {
            mHttpRequest.abort();
        }
    }

    /**
     * 
     * @param objs 如果是手机类型 需要传入 phoneNumber, pwd, verifyCode 邮箱只需传入邮箱地址
     */
    @Override
    public void loadData(Object... objs) {
        if (mType == TYPE_PHOE) {
            loadDataByPhone(objs);
        } else if (mType == TYPE_MAIL) {
            loadDataByMail(objs);
        }
    }

}
